package com.ying.service.order.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.ying.core.exception.CustomException;
import com.ying.core.page.PageData;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ResultCode;
import com.ying.dao.order.OrangeRepository;
import com.ying.po.order.Express;
import com.ying.po.order.Orange;
import com.ying.po.order.Order;
import com.ying.service.order.OrangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class OrangeServiceImpl implements OrangeService {

    @Autowired
    private OrangeRepository orangeRepository;

    @Override
    public Integer create(Orange orange) {
        if (orange.getId() != null) {
            Optional<Orange> optional = orangeRepository.findById(orange.getId());
            if (optional.isPresent()) {
                throw new CustomException(ResultCode.DATA_ALREADY_EXISTED, "OrangeServiceImpl.create");
            }
        }
        return orangeRepository.save(orange).getId();
    }

    @Override
    public void deleteById(Integer id) {
        // 参数检验----------------------------------
        if (id == null)
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "OrangeServiceImpl.deleteById");
        Optional<Orange> optional = orangeRepository.findById(id);
        if (!optional.isPresent()) {
            throw new CustomException(ResultCode.RESULE_DATA_NONE, "OrangeServiceImpl.deleteById");
        }
        // ------------开始业务----------------------
        Orange orange = optional.get();
        orange.setIsDelete(1);
        orangeRepository.save(orange);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        // 参数检验----------------------------------
        if (CollUtil.isEmpty(ids))
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "OrangeServiceImpl.deleteByIdInBatch");
        // ------------开始业务----------------------
        for (Integer id: ids) {
            this.deleteById(id);
        }
    }


    @Override
    public Orange findById(Integer id) {
        // ------------------参数检验---------------
        if (id == null)
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "OrangeServiceImpl.findById");
        // ------------开始业务----------------------
        Optional<Orange> optional = orangeRepository.findByIdAndIsNotDelete(id);
        return optional.orElse(null);
    }

    @Override
    public List<Orange> findByIdInBatch(List<Integer> ids) {
        // ------------------参数检验---------------
        if (CollUtil.isEmpty(ids))
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "OrangeServiceImpl.findByIdInBatch");
        // ------------开始业务----------------------
        List<Orange> orangeList = new ArrayList<Orange>();
        for (Integer id : ids) {
            orangeList.add(this.findById(id));
        }
        return orangeList;
    }

    @Override
    public long countAll() {
        List<Orange> all = new ArrayList<>();
        return all.size();
    }

    @Override
    public List<Orange> findYearAll(String year, Integer userId) {
        List<Orange> all = orangeRepository.findAll();
        List<Orange> records = new ArrayList<>();
        for (Orange orange : all) {
            Date orangeYear = orange.getYear();
            String yearStr = String.valueOf(DateUtil.year(orangeYear));
            if (yearStr.equalsIgnoreCase(year) && orange.getIsDelete() == 0 && Objects.equals(orange.getUserId(), userId)) {
                records.add(orange);
            }
        }
        return records;
    }

    @Override
    public List<Orange> findYearDuring(Integer year1, Integer year2, Integer userId) {
        List<Orange> all = orangeRepository.findAll();
        List<Orange> records = new ArrayList<>();
        for (Orange orange : all) {
            int year = DateUtil.year(orange.getYear());
//            Integer yearStart = Integer.valueOf(year1);
//            Integer yearEnd = Integer.valueOf(year2);
            if (year >= year1 && year<= year2 &&  orange.getIsDelete() == 0 && Objects.equals(orange.getUserId(), userId)) {
                records.add(orange);
            }
        }
        return records;
    }

    @Override
    public List<Map> getOrangePrice(Integer userId) {
        Date date = new Date();
        String thisYear = String.valueOf(DateUtil.year(date));
        List<Orange> orangeList = this.findYearAll(thisYear, userId);
        List<BigDecimal> s = new ArrayList<>();
        List<Map> resultList = new ArrayList<>();
        for (Orange orange : orangeList) {
            if (!s.contains(orange.getPrice()) && thisYear.equals(String.valueOf(DateUtil.year(orange.getYear())))) {
                s.add(orange.getPrice());
                Map map = new HashMap();
                map.put("id", orange.getId());
                map.put("price", orange.getPrice());
                resultList.add(map);
            }
        }
        return resultList;
    }
}
